package com.xfp.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xfp.mapper.UserMapper;
import com.xfp.method.pageTotal;
import com.xfp.pojo.dto.*;
import com.xfp.pojo.entity.Role;
import com.xfp.pojo.entity.User;
import com.xfp.pojo.vo.*;
import com.xfp.service.UserService;
import com.xfp.utils.JwtUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.springframework.util.ObjectUtils;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Service
@Slf4j
public class UserServiceImpl  implements UserService {

    @Autowired
    private UserMapper userMapper;

    /**
     * 用户登录
     * @param dto
     * @return
     * @throws Exception
     */
    @Override
    public LoginUserVo login(LoginDto dto) throws Exception {

        if(dto == null){
            //参数异常
        }
//        String password = encryptToMD5(dto.getPassword());
//
        User user = userMapper.selectOne(dto);
//
//        if(user==null||!user.getPassword().equals(password)){ //代表密码错误
//            throw  new Exception("用户名或密码错误");
//        }
        //密码相等创建vo实例和token
        Map<String,Object> map = new HashMap<>();
        map.put("id",user.getId());
        map.put("userName",user.getUserName());
        String token = JwtUtil.generateJwt(map);

        LoginUserVo loginUserVo = LoginUserVo.builder()
                .userId(user.getId())
                .userName(user.getUserName())
                //.roleCode(user.getRoleCode())
                .success(true)
                .msg("登录成功")
                .token(token)
                .build();
        return loginUserVo;
    }

    @Override
    public List<Role> queryRole(RoleDto dto) {

        List<Role> list = userMapper.queryRole(dto);

        return list;
    }

    /**
     * 人员分页查询实现
     * @param dto
     * @return
     */
    @Override
    public UserQueryVo pageUser(UserQueryDto dto) {
        //分页插件
        PageHelper.startPage(dto.getPageIndex(),dto.getPageSize());
        Page<User> page  = userMapper.pageUser(dto);
        page.forEach(p->{
            Role role = userMapper.findRoleById(p.getRoleId());
            if(!ObjectUtils.isEmpty(role)){
                p.setRegionName(role.getRoleName());
                p.setRoleCode(role.getRoleCode());
                p.setRole(role);
            }
        });
        Long total = pageTotal.total(page.getTotal(), dto.getPageSize());
        return new UserQueryVo(dto.getPageIndex(),dto.getPageSize(),total,page.getTotal(),page.getResult());
    }

    /**
     * 修改人员
     * @param id
     * @param dto
     */
    @Override
    public void updateUser(Long id, UserDto dto) {
        dto.setId(id);
        userMapper.updateUser(dto);
    }

    /**
     * 获取用户基本信息实现
     * @param id
     * @return
     */
    @Override
    public UserVo findUserById(Integer id) {

        UserVo vo  = userMapper.findUserById(id);

        return vo;
    }

    /**
     * 新增人员
     * @param dto
     */
    @Override
    public void insertUser(UserDto dto) {
        userMapper.insertUser(dto);
    }

    /**
     * 删除人员
     * @param id
     */
    @Override
    public void deleteUser(Long id) {
        userMapper.deleteUser(id);
    }


    /**
     * 创建人员工作量
     * @param dto
     * @return
     */
    @Override
    public SearchUserWorkVo searchUserWork(UserWorkPageDto dto) {

        PageHelper.startPage(dto.getPageIndex(),dto.getPageSize());
        Page<User> page = userMapper.searchUserWork(dto);
        SearchUserWorkVo vo = new SearchUserWorkVo();
        vo.setPageIndex(dto.getPageIndex());
        vo.setPageSize(dto.getPageSize());
        vo.setTotalPage(page.getTotal());
        vo.setTotalCount((long) page.getPageNum());
        vo.setCurrentPageRecords(page.getResult());
        return vo;
    }

    /**
     * 根据售货机获取运营人员列表
     * @param innerCode
     * @return
     */
    @Override
    public List<UserOperatorList> operatorList(String innerCode) {
        List<UserOperatorList> list = userMapper.operatorList(innerCode);
        return list;
    }

    /**
     *根据售货机类型获取运维人员列表
     * @param innerCode
     * @return
     */
    @Override
    public List<UserOperatorList> repairerList(String innerCode) {
        List<UserOperatorList> list = userMapper.repairerListList(innerCode);
        return list;
    }

    /**
     * md5加密方法
     * @param str
     * @return
     * @throws UnsupportedEncodingException
     */
    public static String encryptToMD5(String str) throws UnsupportedEncodingException {
        return DigestUtils.md5DigestAsHex(str.getBytes("utf-8"));
    }
}

